/**
 * Created by Administrator on 2017/8/9.
 */
var h = $(window).height(),
    flag = true,
    caseArray = [],
    clueArray = [],
    deleteIndex = 0,
    isUse = 0,
    res = [],
    right = 1,
    subType = 1,
    mxbh = '',
    num = '';
$(function(){
    $('.row-hoof').css({height: h, backgroundColor: '#d5dbe7'});
    $('.newStyle').css({height: h - 20 + 'px'});
    $(window).resize(function(){
        h = $(window).height();
        $('.row-hoof').css({height: h, backgroundColor: '#d5dbe7'});
        $('.newStyle').css({height: h - 20 + 'px'});
    })
    getMsg();
})
function showMore(){
    if(flag){
        $('.bottom-hoof').slideDown();
        flag = false;
        $('.more').removeClass('fa-hand-o-down').addClass('fa-hand-o-up');
    }else{
        $('.bottom-hoof').slideUp();
        flag = true;
        $('.more').removeClass('fa-hand-o-up').addClass('fa-hand-o-down');
    }
}
function showDetail(item){
    if($(item).parent().parent().find('div').css('display') == 'none'){
        $(item).parent().parent().find('div').fadeIn();
    }else{
        $(item).parent().parent().find('div').fadeOut();
    }
    $(item).parents('li').siblings().find('div').css('display','none');
}
function useModel(item){
         if($(item).attr('class').indexOf('fa-pause') == -1){
             $.ajax({
                 type: 'put',
                 url: baseUrls + 'analysis/switchModel',
                 data: {mxbh: $(item).parents('li').children('.mxbh').val()},
                 beforeSend: function(){
                     $('#canvasLoading').show();
                 },
                 success: function(response){
                     $('#canvasLoading').hide();
                     if(response.responseCode == '1000'){
                         getMsg();
                     }else{
                         mark('模型切换失败');
                     }
                 },
                 error: function(){
                     $('#canvasLoading').hide();
                     mark('模型切换失败');
                 }
             })
         }else{
             mark('服务已启用');
         }
}
//模型暂停功能
function stopUse(item){
    if($(item).attr('class').indexOf('fa-pause') > -1){
        $(item).addClass('fa-play').removeClass('fa-pause');
        $('.load-hoof').children('.loadAnimate-hoof').removeClass('loadAnimate');
        $('.load-hoof').children('.loadWord-hoof').html('暂停中');
        for(var i = 0; i < $('.list .name').length; i++){
            if($('.list .name').eq(i).html().indexOf($('.model-hoof').html()) > -1){
                $('.list li').eq(i).children('.user').children('.controlModel').addClass('fa-play').removeClass('fa-pause');
            }
        }
    } else{
        $('.load-hoof').children('.loadAnimate-hoof').addClass('loadAnimate');
        $('.load-hoof').children('.loadWord-hoof').html('我正在努力研判...');
        $('.stop-hoof').addClass('fa-pause').removeClass('fa-play');
        for(var i = 0; i < $('.list .name').length; i++){
            if($('.list .name').eq(i).html().indexOf($('.model-hoof').html()) > -1){
                $('.list li').eq(i).children('.user').children('.controlModel').addClass('fa-pause').removeClass('fa-play');
            }
        }
    }
}
function addInfo(item){
    if($(item).parent().attr('class').indexOf('ul-one-hoof') > -1){
        if(caseArray.indexOf($(item).find('input').val()) > -1){
            mark('您已添加');
        } else{
            $('.ul-three-hoof').append('<li onclick="removeInfo(this)"><span class="spanContent">' + $(item).children('.spanContent').html() + '</span>&nbsp&nbsp<span class="fa fa-minus" aria-hidden="true"></span><input type="hidden" value="'+ $(item).find('input').val() +'"></li>');
            caseArray.push($(item).find('input').val());
        }
    }else{
        if(clueArray.indexOf($(item).find('input').val()) > -1){
            mark('您已添加');
        } else{
            $('.ul-four-hoof').append('<li onclick="removeInfo(this)"><span class="spanContent">' + $(item).children('.spanContent').html() + '</span>&nbsp&nbsp<span class="fa fa-minus" aria-hidden="true"></span><input type="hidden" value="'+ $(item).find('input').val() +'"></li>');
            clueArray.push($(item).find('input').val());
        }
    }
    $('.createLeft:eq(3)').css('line-height', $('.createRight:eq(3)').height()+ 'px');
}
function removeInfo(item){
    if($(item).parent().attr('class').indexOf('ul-three-hoof') > -1){
        var index = caseArray.indexOf($(item).find('input').val());
        caseArray.splice(index,1);
    }else{
        var index = clueArray.indexOf($(item).find('input').val());
        clueArray.splice(index,1);
    }
    $('.createLeft:eq(3)').css('line-height', $('.createRight:eq(3)').height() - 20 + 'px');
    $(item).remove();
}
function changeTip(item){
    subType = 2;
    $(item).addClass('btn-active').removeClass('btn-normal').siblings().addClass('btn-normal').removeClass('btn-active');
    if($(item).index() == 0){
        $('.list').show().siblings().hide();
    }else{
        if(right == 1){
            getList();
            $('.createModel').show().siblings().hide();
            $('.input-hoof').val('');
            $('.ul-three-hoof').html('');
            $('.ul-four-hoof').html('');
            caseArray = [];
            clueArray = [];
            right = 2;
        }else{
            $('.createModel').show().siblings().hide();
            $('.input-hoof').val('');
            $('.ul-three-hoof').html('');
            $('.ul-four-hoof').html('');
            caseArray = [];
            clueArray = [];
        }
    }
    for(var i = 0; i < $('.listDetail').length; i++){
        $('.listDetail').eq(i).fadeOut();
    }
    $('.createLeft:eq(3)').css('line-height', $('.createRight:eq(3)').height()+ 'px');
}
function Submit(){
    if($.trim($('.input-hoof').val()).length > 0 && $('#createKinds li').length > 0){
        $('.list').show().siblings().hide();
        $('.btns:eq(0)').addClass('btn-active').removeClass('btn-btn-normal').siblings().addClass('btn-normal').removeClass('btn-active');
        if(subType == 2){
            var fileString = caseArray.concat(clueArray).sort(function(a,b){return a - b}).join(',');
            $.ajax({
                type: 'post',
                url: baseUrls + 'analysis/model',
                data: {mxmc: $('.input-hoof').val(), jcx: fileString},
                beforeSend: function(){
                    $('#canvasLoading').show();
                },
                success: function(response){
                    $('#canvasLoading').hide();
                    if(response.responseCode == '1000'){
                        getMsg();
                    }else{
                        mark('研判模型添加失败');
                    }
                },
                error: function(){
                    $('#canvasLoading').hide();
                    mark('研判模型添加失败');
                }
            })
        }else{
            var fileString = caseArray.concat(clueArray).sort(function(a,b){return a - b}).join(',');
            $.ajax({
                type: 'put',
                url: baseUrls + 'analysis/model',
                data: {mxmc: $('.input-hoof').val(), jcx: fileString,mxbh: mxbh},
                beforeSend: function(){
                    $('#canvasLoading').show();
                },
                success: function(response){
                    $('#canvasLoading').hide();
                    if(response.responseCode == '1000'){
                        getMsg();
                    }else{
                        mark('研判模型添加失败');
                    }
                },
                error: function(){
                    $('#canvasLoading').hide();
                    mark('研判模型添加失败');
                }
            })
        }
        $('.input-hoof').val('');
        $('.ul-three-hoof').html('');
        $('.ul-four-hoof').html('');
        caseArray = [];
        clueArray = [];
        for(var i = 0; i < $('.listDetail').length; i++){
            $('.listDetail').eq(i).fadeOut();
        }
    }else{
        mark('请输入模型名称和内容不能为空');
    }
}
function edit(item){
    subType = 1;
    mxbh = $(item).parents('li').children('.mxbh').val();
    if(right == 1){
        getList();
        right = 2;
    }
    $('.btns').eq(1).addClass('btn-active').removeClass('btn-btn-normal').siblings().addClass('btn-normal').removeClass('btn-active');
    $('.createModel').show().siblings().hide();
    var data = $(item).parents('li').children('.listDetail').children('.group').children('.form-group').children('.case-hoof').find('span'),
        datas = $(item).parents('li').children('.listDetail').children('.group').children('.form-group').children('.clue-hoof').find('span');
    $('.ul-three-hoof').html('');
    $('.ul-four-hoof').html('');
    for(var i = 0; i < $(data).length; i++){
        $('.ul-three-hoof').append('<li onclick="removeInfo(this)"><span class="spanContent">' + data[i].innerHTML + '</span>&nbsp&nbsp<span class="fa fa-minus" aria-hidden="true"></span></li>');
        caseArray.push($(data[i]).find('input').val());
    }
    for(var i = 0; i < $(datas).length; i++){
        $('.ul-four-hoof').append('<li onclick="removeInfo(this)"><span class="spanContent">' + datas[i].innerHTML + '</span>&nbsp&nbsp<span class="fa fa-minus" aria-hidden="true"></span></li>');
        clueArray.push($(datas[i]).find('input').val());
    }
    $('.createLeft:eq(3)').css('line-height', $('.createRight:eq(3)').height()+ 'px');
    $('.input-hoof').val($(item).parents('li').children('.name').html());
}
function getList(){
    $.ajax({
        type: 'get',
        url: baseUrls + 'analysis/analyseItems',
        beforeSend: function(){
            $('#canvasLoading').show();
        },
        success: function(response){
            $('#canvasLoading').hide();
            if(response.responseCode == '1000'){
                if(response.content.length > 0){
                    var caseList = '',
                        clueList = '';
                    for(var i = 0; i < response.content.length; i++){
                        if(response.content[i].jclb == 0){
                            caseList += '<li onclick="addInfo(this)"><span class="spanContent">'+ response.content[i].jcxms +'</span>&nbsp&nbsp<span class="fa fa-plus" aria-hidden="true"></span><input type="hidden" class="jcxbh" value="'+ response.content[i].jcxbh +'"></li>';
                        }else if(response.content[i].jclb == 1){
                            clueList += '<li onclick="addInfo(this)"><span class="spanContent">'+ response.content[i].jcxms +'</span>&nbsp&nbsp<span class="fa fa-plus" aria-hidden="true"></span><input type="hidden" class="jcxbh" value="'+ response.content[i].jcxbh +'"></li>';
                        }
                    }
                    $('.allMsg').html('<div class="group groups"><div class="createLeft">案件</div><div class="createRight"><div class="createKinds"><ul class="clearfix ul ul-one-hoof">'+ caseList +'</ul></div></div></div><div class="group groups"><div class="createLeft">线索</div><div class="createRight"><div class="createKinds"><ul class="clearfix ul ul-two-hoof">'+ clueList +'</ul></div></div></div>');
                    $('.createLeft:eq(0)').css('line-height', $('.createRight:eq(0)').height() - 15 + 'px');
                    $('.createLeft:eq(1)').css('line-height', $('.createRight:eq(1)').height() - 15 + 'px');
                    $('.createLeft:eq(2)').css('line-height', $('.createRight:eq(2)').height() - 15 + 'px');
                }else{
                    $('.allMsg').html('<div class="group groups"><div class="createLeft">案件</div><div class="createRight"><div class="createKinds"><ul class="clearfix ul ul-one-hoof"></ul></div></div></div><div class="group groups"><div class="createLeft">线索</div><div class="createRight"><div class="createKinds"><ul class="clearfix ul ul-two-hoof"></ul></div></div></div>');
                }
            }else{
                mark('模型项获取失败');
            }
        },
        error: function(){
            $('#canvasLoading').hide();
            mark('模型项获取失败');
        }
    })
}
function dele(item){
    var title = '';
    if($(item).parents('li').children('.name').html() == $('.model-hoof').html()){
        title = '确认删除使用的对比模型吗？';
        isUse = 1;
    } else{
        title = '确认删除吗?';
        isUse = 2;
    }
    deleteIndex = $(item).parents('li').index();
    layer.open({
        type: 1,
        title: title, //不显示标题栏
        closeBtn: false,
        area: '300px;',
        shade: 0.8,
        id: 'LAY_layuipro', //设定一个id，防止重复弹出
        resize: false,
        btn: ['删除', '取消'],
        btnAlign: 'c',
        moveType: 1, //拖拽模式，0或者1
        content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除前请确保已关闭研判！<br>可联系现场人员确认！</div>',
        success: function(layero){
            var btn = layero.find('.layui-layer-btn');
            btn.find('.layui-layer-btn0').attr({
                onClick: 'Sure()'
            });
        }
    });
}
function getMsg(){
    $.ajax({
        type: 'get',
        url: baseUrls + 'analysis/model',
        data:'',
        beforeSend: function(){
            Loading();
        },
        success: function(response){
            closeLoading();
            if(response.responseCode == '1000'){
                if(response.content.models.length > 0){
                    res = response.content.models;
                    num = response.content.count;
                    $('#list').html('');
                    for(var i = 0; i < response.content.models.length; i++){
                        var caseData = '',
                            clueData = '';
                        for(var j = 0; j < response.content.models[i].itemList.length; j++){
                            if(response.content.models[i].itemList[j].jclb == 1){
                                clueData += '<span>'+ response.content.models[i].itemList[j].jcxms +'<input type="hidden" value="'+ response.content.models[i].itemList[j].jcxbh +'"></span>'
                            }else{
                                caseData += '<span>'+ response.content.models[i].itemList[j].jcxms +'<input type="hidden" value="'+ response.content.models[i].itemList[j].jcxbh +'"></span>'
                            }
                        }
                        if(response.content.models[i].mxzt == 0){
                            $('#list').append('<li><span class="num bigSpan">'+ (i + 1) +'</span><span class="name bigSpan">'+ response.content.models[i].mxmc +'</span><span class="status bigSpan">未启用</span><span class="set bigSpan"><span class="fa fa-pencil-square-o" aria-hidden="true" title="点击编写" onclick="edit(this)" style="color: #155789"></span>&nbsp&nbsp<span class="fa fa-trash" aria-hidden="true" title="点击删除" onclick="dele(this)" style="color: red"></span>&nbsp&nbsp<span class="fa fa-question-circle" aria-hidden="true" title="点击查看详情" onclick="showDetail(this)" style="color: #17cd5e"></span></span><span class="time bigSpan">'+ response.content.models[i].cjsj +'</span><span class="user bigSpan"><span class="fa fa-play controlModel" onclick="useModel(this)"></span></span><div class="listDetail"><div class="group"><div class="form-group"><label>案件项：</label><div class="detail case-hoof">'+ caseData +'</div></div><div class="form-group"><label>线索项：</label><div class="detail clue-hoof">'+ clueData +'</div></div></div></div><input type="hidden" class="mxbh" value="'+ response.content.models[i].mxbh +'"></li>');
                        }else{
                            $('#list').append('<li><span class="num bigSpan">'+ (i + 1) +'</span><span class="name bigSpan">'+ response.content.models[i].mxmc +'</span><span class="status bigSpan spanColor">启用中</span><span class="set bigSpan"><span class="fa fa-pencil-square-o" aria-hidden="true" title="点击编写" onclick="edit(this)" style="color: #155789"></span>&nbsp&nbsp<span class="fa fa-trash" aria-hidden="true" title="点击删除" onclick="dele(this)" style="color: red"></span>&nbsp&nbsp<span class="fa fa-question-circle" aria-hidden="true" title="点击查看详情" onclick="showDetail(this)" style="color: #17cd5e"></span></span><span class="time bigSpan">'+ response.content.models[i].cjsj +'</span><span class="user bigSpan"><span class="fa fa-pause controlModel" onclick="useModel(this)"></span></span><div class="listDetail"><div class="group"><div class="form-group"><label>案件项：</label><div class="detail case-hoof">'+ caseData +'</div></div><div class="form-group"><label>线索项：</label><div class="detail clue-hoof">'+ clueData +'</div></div></div></div><input type="hidden" class="mxbh" value="'+ response.content.models[i].mxbh +'"></li>');
                            $('#top').html('<li class="modelLi"><span class="fa fa-diamond" aria-hidden="true"></span>&nbsp&nbsp<span class="model model-hoof">'+ response.content.models[i].mxmc +'</span></li><li class="timesLi"><span class="times times-hoof">'+ response.content.count +'次</span></li><li class="timeLi"><span class="time time-hoof">'+ response.content.models[i].cjsj +'</span></li><li class="loadLi load-hoof"><span class="loadAnimate fa fa-spinner loadAnimate-hoof" aria-hidden="true"></span>&nbsp&nbsp<span class="loadWord loadWord-hoof">我正在努力研判...</span></li><li class="stopLi"><span class="stop fa fa-pause stop-hoof" aria-hidden="true"></span></li>');
                            $('.bottom-hoof').html('<div class="group"><div class="form-group"><label>案件项：</label><div class="detail">'+ caseData +'</div></div><div class="form-group"><label>线索项：</label><div class="detail">'+ clueData +'</div></div></div>');
                        }
                    }
                }else{
                    mark('暂无数据');
                }
            }else{
                mark('数据获取失败');
            }
        },
        error: function(){
            closeLoading();
        }
    })
}
function Sure(){
    $.ajax({
        type: 'put',
        url: baseUrls + 'analysis/model',
        data: {mxbh: $('.list ul li').eq(deleteIndex).children('.mxbh').val(), mxzt: 2},
        beforeSend: function(){
            Loading();
        },
        success: function(response){
            closeLoading();
            if(response.responseCode == '1000'){
                $('.list ul li').eq(deleteIndex).remove();
                if(isUse == 1){
                    $('#top').html('');
                    // $('.model-hoof').html('');
                    // $('.times-hoof').html('');
                    // $('.time-hoof').html('');
                    // $('.loadAnimate-hoof').removeClass('loadAnimate');
                    // $('.loadWord-hoof').html('无');
                    // $('.stop-hoof').attr('class','stop fa');
                    $('.bottom-hoof').html('<div class="group"><div class="form-group"><label>案件项：</label><div class="detail"></div></div><div class="form-group"><label>线索项：</label><div class="detail"></div></div></div>');
                    $('.bottom-hoof').slideUp();
                    flag = true;
                    $('.more').removeClass('fa-hand-o-up').addClass('fa-hand-o-down');
                }
                $('#list').html('');
                res.splice(deleteIndex,1);
                for(var i = 0; i < res.length; i++){
                    var caseData = '',
                        clueData = '';
                    for(var j = 0; j < res[i].itemList.length; j++){
                        if(res[i].itemList[j].jclb == 1){
                            clueData += '<span>'+ res[i].itemList[j].jcxms +'</span>'
                        }else{
                            caseData += '<span>'+ res[i].itemList[j].jcxms +'</span>'
                        }
                    }
                    if(res[i].mxzt == 0){
                        $('#list').append('<li><span class="num bigSpan">'+ (i + 1) +'</span><span class="name bigSpan">'+ res[i].mxmc +'</span><span class="status bigSpan">未启用</span><span class="set bigSpan"><span class="fa fa-pencil-square-o" aria-hidden="true" title="点击编写" onclick="edit(this)" style="color: #155789"></span>&nbsp&nbsp<span class="fa fa-trash" aria-hidden="true" title="点击删除" onclick="dele(this)" style="color: red"></span>&nbsp&nbsp<span class="fa fa-question-circle" aria-hidden="true" title="点击查看详情" onclick="showDetail(this)" style="color: #17cd5e"></span></span><span class="time bigSpan">'+ res[i].cjsj +'</span><span class="user bigSpan"><span class="fa fa-play controlModel" onclick="useModel(this)"></span></span><div class="listDetail"><div class="group"><div class="form-group"><label>案件项：</label><div class="detail case-hoof">'+ caseData +'</div></div><div class="form-group"><label>线索项：</label><div class="detail clue-hoof">'+ clueData +'</div></div></div></div><input type="hidden" class="mxbh" value="'+ res[i].mxbh +'"></li>');
                    }else{
                        $('#list').append('<li><span class="num bigSpan">'+ (i + 1) +'</span><span class="name bigSpan">'+ res[i].mxmc +'</span><span class="status bigSpan spanColor">启用中</span><span class="set bigSpan"><span class="fa fa-pencil-square-o" aria-hidden="true" title="点击编写" onclick="edit(this)" style="color: #155789"></span>&nbsp&nbsp<span class="fa fa-trash" aria-hidden="true" title="点击删除" onclick="dele(this)" style="color: red"></span>&nbsp&nbsp<span class="fa fa-question-circle" aria-hidden="true" title="点击查看详情" onclick="showDetail(this)" style="color: #17cd5e"></span></span><span class="time bigSpan">'+ res[i].cjsj +'</span><span class="user bigSpan"><span class="fa fa-pause controlModel" onclick="useModel(this)"></span></span><div class="listDetail"><div class="group"><div class="form-group"><label>案件项：</label><div class="detail case-hoof">'+ caseData +'</div></div><div class="form-group"><label>线索项：</label><div class="detail clue-hoof">'+ clueData +'</div></div></div></div><input type="hidden" class="mxbh" value="'+ res[i].mxbh +'"></li>');
                        $('#top').html('<li class="modelLi"><span class="fa fa-diamond" aria-hidden="true"></span>&nbsp&nbsp<span class="model model-hoof">'+ res[i].mxmc +'</span></li><li class="timesLi"><span class="times times-hoof">'+ num +'次</span></li><li class="timeLi"><span class="time time-hoof">'+ res[i].cjsj +'</span></li><li class="loadLi load-hoof"><span class="loadAnimate fa fa-spinner loadAnimate-hoof" aria-hidden="true"></span>&nbsp&nbsp<span class="loadWord loadWord-hoof">我正在努力研判...</span></li><li class="stopLi"><span class="stop fa fa-pause stop-hoof" aria-hidden="true"></span></li>');
                        $('.bottom-hoof').html('<div class="group"><div class="form-group"><label>案件项：</label><div class="detail">'+ caseData +'</div></div><div class="form-group"><label>线索项：</label><div class="detail">'+ clueData +'</div></div></div>');
                    }
                }
            }else{
                mark('删除失败');
            }
        },
        error: function(){
            closeLoading();
            mark('删除失败');
        }
    })
}
function mark(item){
    $('.H3').html(item);
    $('.mark').fadeIn();
    setTimeout(function(){
        $('.mark').fadeOut();
    },1000)
}